package com.demo.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 统一的AOP管理
 * Created by chenjun on 2016/7/4 0004.
 */
@Aspect
@Component
public class UnifyAop {
    private Logger logger = LoggerFactory.getLogger(UnifyAop.class);
    @Autowired
    private HttpServletRequest request;

    /**
     * controller的切入点
     */
    @Pointcut("execution(* com.demo.controller*..*(..))")
    private void controllerMethod() {
    }


    /**
     * controller层aop处理
     *
     * @param point
     * @return
     * @throws Throwable
     */
    @Around(value = "controllerMethod()")
    public Object controllerAround(ProceedingJoinPoint point) throws Throwable {
        Object[] args = point.getArgs();
        String typeName = point.getSignature().getDeclaringTypeName();
        String methodName = point.getSignature().getName();
        Object result = point.proceed();
        return result;
    }

    /**
     * 获取错误字段信息
     *
     * @param result
     * @return
     */
    private String getParamErrorsMessage(BindingResult result) {
        List<ObjectError> errors = result.getAllErrors();
        StringBuffer msg = new StringBuffer("");
        for (ObjectError error : errors) {
            msg.append(error.getDefaultMessage()).append("；");
        }
        return msg.toString();
    }
}
